﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using DataBase.Utils;
using GalaSoft.MvvmLight.Command;
using GreenXi.Model;

namespace GreenXi.ViewModel
{

    public class LogListViewModel : LogListModel
    {
        public LogListViewModel()
        {

            var list = MyDbContext.Instance.ControlLog.OrderByDescending(x => x.Id).ToList();
            OPDate = list;
            if (OPDate != null && OPDate.Count > 0)
            {
                LogList = OPDate;
                DataListCount = Convert.ToUInt32(OPDate.Count);
            }
            else
            {
                LogList = OPDate;
                DataListCount = 1;
            }


            DataListPagingStartLengthChangeEvent += DataItemViewModel_DataListPagingStartLengthChangeEvent;
        }
        #region   分页方法
        /// <summary>
        /// 数据总数
        /// </summary>
        private uint _dataListCount = 20;
        public uint DataListCount
        {
            get { return _dataListCount; }
            set
            {
                SetProperty(ref _dataListCount, value);
                RaisePropertyChanged("DataListCount");
            }
        }

        /// <summary>
        ///(分页事件) 用于界面绑定的 页面刷新事件
        /// </summary>
        public ICommand PageRefreshCmd
        {
            get
            {
                return new RelayCommand<Tuple<uint, uint>>(tuple =>
                {
                    if (DataListPagingStartLengthChangeEvent != null)
                    {
                        DataListPagingStartLengthChangeEvent(tuple.Item1,
                                                             tuple.Item2);
                    }
                });
            }
        }
        /// <summary>
        /// 数据列表刷新的   请求事件，参数-startIndex,length
        /// </summary>
        public event Action<uint, uint> DataListPagingStartLengthChangeEvent;
        private void DataItemViewModel_DataListPagingStartLengthChangeEvent(uint start, uint length)
        {
            if (LogList != null)
            {
                //从新给数据源赋值
                var len = (int)length;
                int star = (int)(start - 1) * len;
                LogList = OPDate.Skip(star).Take(len).ToList();
            }

        }
        #endregion
    }
}
